﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.UserManagement;
using System.Configuration;
using SIPO.Web.Support;
using System.Globalization;


namespace SIPO.Web.Forms.Transaction
{
    public partial class recPaymentform : BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {
            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
                hfID.Value = id.ToString();
            }

            if (!Page.IsPostBack)
            {

                if (id > 0) // Edit an existing item
                {
                    ddlBank.Enabled = true;
                    ddlNoInvoice.Enabled = true;

                    recBankAccountRepo recBankAccountDal = new recBankAccountRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    List<recBankAccount> banks = recBankAccountDal.GetList();
                    ddlBank.Items.Clear();
                    banks.ForEach(delegate(recBankAccount bank)
                    {
                        ddlBank.Items.Add(new ListItem(bank.BankCode + " - " + bank.BankName, bank.BankID.ToString()));
                    });

                    ddlBank.Items.Insert(0, new ListItem("-- pilih bank --", string.Empty));

                    recInvoiceRepo recInvoiceDal = new recInvoiceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    List<recInvoice> invoices = recInvoiceDal.GetList();
                    ddlNoInvoice.Items.Clear();
                    invoices.ForEach(delegate(recInvoice invoice)
                    {
                        ddlNoInvoice.Items.Add(new ListItem(invoice.InvoiceNo, invoice.InvoiceNo.ToString()));
                    });

                    ddlNoInvoice.Items.Insert(0, new ListItem("-- pilih surat setoran --", string.Empty));

                    recPaymentRepo recPaymentDal = new recPaymentRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recPayment obj = recPaymentDal.GetById(id);

                    txtJumlahPembayaran.Text = obj.PaymentValue.ToString();

                    txtTanggalPembayaran.Text = obj.PaymentDate.ToString("dd/MM/yyyy");
                    ddlNoInvoice.Items.FindByValue(obj.InvoiceNo.ToString()).Selected = true;


                    recDebtorSectorRepo recDebtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recDebtorSector objDebtorSector = recDebtorSectorDal.GetById(Convert.ToInt32(obj.DebtorSectorID));

                    if (objDebtorSector != null)
                    {
                        recDebtorRepo recDebtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                        recDebtor objDebtor = recDebtorDal.GetById(objDebtorSector.DebtorID);
                        if (objDebtor != null)
                            txtDebitur.Text = objDebtor.DebtorName;
                    }

                    recInvoice objInvoice = recInvoiceDal.GetById(obj.InvoiceNo);
                    if (objInvoice != null)
                        ddlBank.Items.FindByValue(objInvoice.BankID.ToString()).Selected = true;

                    if (obj.DebtorSectorID == 0)
                        txtDebitur.Text = obj.Remark;

                    hfDebtorSectorID.Value = obj.DebtorSectorID.ToString();

                }

               GetPrivilege();


            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').hide();", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    }
}